// --------------------------
// -- Debugging routines ----
// --------------------------
function debug(evt) {
  var str,
      holder = document.getElementsByTagName("body").item(0),
      container = document.createElement("pre"),
      styles = {"textAlign":"left",
                "background":"#fff",
                "border":"1px solid #333",
                "padding":"2%",
                "width":"80%",
                "overflow":"auto",
                "margin":"1em auto"};
  for (i in evt) { str += i + ":" + evt[i] + "\n"; }    
  container.appendChild(document.createTextNode(str));
  defineCSS(container, styles);
  holder.appendChild(container);
}


// --------------------------
// ------- DIY Legacy -------
// --------------------------

var undefined;

if (typeof Function.prototype.call == undefined) {
  Function.prototype.call = function(obj, param) {
    obj.base = this;
    obj.base(param);  
  }  
}

// ---------------------------
// -- Standard DOM Routines --
// ---------------------------
function addLoadEvent(func){var oldonload=window.onload;window.onload=(typeof window.onload!='function')?func:function(){oldonload();func();}}
function addEvent(obj,evType,fn,useCapture){if(obj.addEventListener){obj.addEventListener(evType,fn,useCapture);return true;}else if(obj.attachEvent){var r=obj.attachEvent("on"+evType,fn);return r;}else{return null;}} 
function removeEvent(obj,evType,fn,useCapture){if(obj.removeEventListener){obj.removeEventListener(evType,fn,useCapture);return true;}else if(obj.detachEvent){var r=obj.detachEvent("on"+evType,fn);return r;}else{return null;}}

function defineCSS(obj, styles) {
  if (styles instanceof Object) {
    for (var prop in styles) {
      obj.style[prop] = styles[prop];    
    }  
  }  
}

// ----------------------------
// -- Prototype enhancements --
// ----------------------------
Function.prototype.Iterate = function(collection) {
  for (var i = 0, elem; elem = collection.item(i++); ) {
    this(elem);	
  }  
}


/**
 * Base Prototype for Event Listeners
 *
 * @abstract
 */
function EventListener(elem) {
  
  this.elem = elem;
  this.evt = "";
    
  this.invoke = function(evt) {
    this.evt = (evt) ? evt :
      ((window.event) ? window.event : null);
  }  
  
  this.cancelDefault = function() {
    if (this.evt) {
      if (this.evt.preventDefault) {
        this.evt.preventDefault();
      }
      this.evt.returnValue = false;        
    }
  }
  
  this.register = function(handler,fn) {
    if (handler instanceof Array) {
      for(var i=0;i<handler.length; i++) {
        addEvent(this.elem,handler[i],fn);
      }
    }
    else { addEvent(this.elem,handler,fn); }
  }  
  
  this.unregister = function(handler, fn) {
    if (handler instanceof Array) {
      for (var i = 0; i < handler.length; i++) {      
        removeEvent(this.elem, handler[i], fn);  
      }    
    }
    else { removeEvent(this.elem, handler, fn); }
  }   
  
}

/**
 * @added     07/01/2005 00:55
 * Should move this elsewhere but temp here for now
 */
addLoadEvent( function() {
  var tables = document.getElementsByTagName('table'), elem, rows;
  if (tables && tables.length > 0) {
    (function(elem) {
      if (elem.id != "calendar") {
        var rows = elem.rows;
        for (var i = 0, m = rows.length; i < m; i++) {
          if (i%2 == 0) {
            rows[i].style.backgroundColor = "#eaeaea"; 
          }  
          else {
            rows[i].style.backgroundColor = "#f5f5f5";            
          }            
        }
      }  
    }).Iterate(tables);  
  } 
});

// clean up routine for WinIE to avoid memory leaks
// this can take around 0.6s for 500+ DOM nodes!
if(typeof window.attachEvent != 'undefined')
{
  window.attachEvent('onunload', function()
  {
    // loop through every item in the document collection
    // var start = new Date();
    for(var i=0,m=document.all.length;i<m;i++) {
      document.all[i]['onmouseover'] = null;
      document.all[i]['onmouseout'] = null;      
      document.all[i]['onmousemove'] = null;      
      document.all[i]['onfocus'] = null;      
      document.all[i]['onblur'] = null;      
      document.all[i]['onclick'] = null;      
    }
    // var end = new Date();
    // var time = end.getTime() - start.getTime();
    // alert(i + " DOM nodes cleaned.\nClean up took " + time + "ms.");
  });
}